/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rcnss.data;
import java.sql.*;
import java.util.*;
/**
 *
 * @author Guesmi
 *
 */

public class User extends ITable {
    
    //public String UTILISATEUR ;
    private static final String Table = "utilisateur";

    /* parameters*/
    private int UTL_ID ;
    private long UTL_MAT ;
    private String UTL_NOM ;
    private String UTL_PRENOM ;
    private String UTL_LOGIN ;
    private String UTL_PWD ;
    private int UTL_POST ;
    private int UTL_TYPE ;
    private int UTL_TEL_B ;
    private int UTL_TEL_MOBILE ;
    private String UTL_MAIL ;
    private int STR_CODE;

    public static String[] Post = {"Ingenieur", "Technecien"};
    public static String[] Type = {"Adminstrateur", "Technicien", "Utilisateur Simple", "Visiteur"};


    public int getID(){return this.UTL_ID;}
    public void setID(int id){this.UTL_ID = id ;}
    public int getCodeStructure() {
        return this.STR_CODE;
    }
    public void setCodeStructure(int code) {
        if(Structure.SearchByID(code)!=null){
            this.STR_CODE = code;
        }
    }
    public void setStructure(Structure s) {
        setCodeStructure(s.GetCODE());
    }
    public int getPOST() {
        return UTL_POST;
    }
    public void setPOST(int UTL_POST) {
        this.UTL_POST = UTL_POST;
    }
    public long getMatricule() {
        return UTL_MAT;
    }
    public void setMatricule(long UTL_MAT) {
        this.UTL_MAT = UTL_MAT;
    }
    public String getNOM() {
        return UTL_NOM;
    }
    public void setNOM(String UTL_NOM) {
        this.UTL_NOM = UTL_NOM;
    }
    public String getPrenom(){return this.UTL_PRENOM;}
    public void setPrenom(String prenom){this.UTL_PRENOM = prenom;}
    public String getPWD() {
        return UTL_PWD;
    }
    public void setPWD(String UTL_PWD) {
        this.UTL_PWD = UTL_PWD;
    }
    public String getLogin(){return this.UTL_LOGIN;}
    public void setLogin(String login){this.UTL_LOGIN = login;}
    public String getMail(){return this.UTL_MAIL;}
    public void setMail(String mail){this.UTL_MAIL = mail ;}
    public int getTelBureau(){return this.UTL_TEL_B;}
    public void setTelBureau(int bur){this.UTL_TEL_B = bur ;}
    public int getTelMobile(){return this.UTL_TEL_MOBILE;}
    public void setTelMobile(int mob){this.UTL_TEL_MOBILE = mob ;}
    public int getTypeUser() {
        return(this.UTL_TYPE);
    }
    public void setTypeUser(int type) {
        this.UTL_TYPE = type;
    }

    /*constructeur*/
    public User(){};

    public void LoadFromDB (String MAT){
        try{
        ResultSet res = db.executerSelect(" Select * from "+User.Table+" where UTL_MAT ='" + MAT + "'");
        if(res.next())
             {
                this.UTL_ID = res.getInt("UTL_ID");
                this.UTL_NOM = res.getString("UTL_NOM");
                this.UTL_PRENOM = res.getString("UTL_PRENOM");
                this.UTL_PWD = res.getString("UTL_PWD");
                this.UTL_LOGIN = res.getString("UTL_LOGIN");
                this.UTL_MAIL = res.getString("UTL_MAIL");
                this.UTL_MAT = res.getInt("UTL_MAT");
                this.UTL_POST = res.getInt("UTL_POST");
                this.UTL_TYPE = res.getInt("UTL_TYPE");
                this.UTL_TEL_B = res.getInt("UTL_TEL_B");
                this.UTL_TEL_MOBILE = res.getInt("UTL_TEL_MOBILE");
                this.STR_CODE = res.getInt("STR_CODE");
             }
        else
                System.out.println("offff .. erreur!!");
        }catch(Exception sqle){}

    }

    public void SaveToDB() throws Exception{
        if(SearchByID(this.UTL_ID)!=null) {
            ChangeUser();
        } else {
        db.InsertNewLine(User.Table,
                "UTL_ID",Integer.toString(this.UTL_ID),
                "UTL_MAT",Long.toString(this.UTL_MAT),
                "UTL_NOM","'"+this.UTL_NOM+"'",
                "UTL_PRENOM","'"+this.UTL_PRENOM+"'",
                "UTL_PWD","'"+ this.UTL_PWD +"'",
                "UTL_LOGIN","'"+this.UTL_LOGIN+"'",
                "UTL_MAIL","'"+this.UTL_MAIL+"'",
                "UTL_TEL_B",Integer.toString(this.UTL_TEL_B),
                "UTL_TEL_MOBILE",Integer.toString(this.UTL_TEL_MOBILE),
                "UTL_POST",Integer.toString(this.UTL_POST),
                "STR_CODE",Integer.toString(this.STR_CODE),
                "UTL_TYPE",Integer.toString(this.UTL_TYPE));
        }
    }

    private void  ChangeUser() throws Exception{
        db.UpdateLine(User.Table,
                this.UTL_ID,
                    "UTL_ID",Integer.toString(this.UTL_ID),
                    "UTL_MAT",Long.toString(this.UTL_MAT),
                    "UTL_NOM","'"+this.UTL_NOM+"'",
                    "UTL_PRENOM","'"+this.UTL_PRENOM+"'",
                    "UTL_PWD","'"+ this.UTL_PWD +"'",
                    "UTL_LOGIN","'"+this.UTL_LOGIN+"'",
                    "UTL_MAIL","'"+this.UTL_MAIL+"'",
                    "UTL_TEL_B",Integer.toString(this.UTL_TEL_B),
                    "UTL_TEL_MOBILE",Integer.toString(this.UTL_TEL_MOBILE),
                    "UTL_POST",Integer.toString(this.UTL_POST),
                    "STR_CODE",Integer.toString(this.STR_CODE),
                    "UTL_TYPE",Integer.toString(this.UTL_TYPE));
    }

    @Override
    public String toString()
    {
        return this.UTL_NOM + " " + this.UTL_PRENOM;
    }

/*******************************************************************************
 *              STATIC FUNCTIONS
 * ****************************************************************************/


    
    public static User SearchByID(int ID)
    {
        User u = new User();
        ConnexionBD db = new ConnexionBD ();
        try{
            ResultSet res = db.executerSelect("select * from "+ User.Table +" where UTL_ID ='"+ ID +"'");
            while (res.next())
            {
            u.UTL_ID = res.getInt("UTL_ID");
            u.UTL_POST = res.getInt("UTL_POST");
            u.UTL_MAT = res.getInt("UTL_MAT");
            u.UTL_NOM = res.getString("UTL_NOM");
            u.UTL_PRENOM = res.getString("UTL_PRENOM");
            u.UTL_MAIL = res.getString("UTL_MAIL");
            u.UTL_PWD = res.getString("UTL_PWD");
            u.UTL_LOGIN = res.getString("UTL_LOGIN");
            u.UTL_TYPE = res.getInt("UTL_TYPE");
            u.UTL_TEL_B = res.getInt("UTL_TEL_B");
            u.UTL_TEL_MOBILE = res.getInt("UTL_TEL_MOBILE");
            u.STR_CODE = res.getInt("STR_CODE");
            }
        }catch(Exception sqle){u = null; }
        return u;
    }

    public static ArrayList<User> GetAll()
    {
        ArrayList<User> list = new ArrayList<User>();
        ConnexionBD db = new ConnexionBD ();
        try{
        ResultSet res = db.executerSelect("select * from "+ User.Table);
        while (res.next())
        {
            User u = new User();
            u.UTL_ID = res.getInt("UTL_ID");
            u.UTL_POST = res.getInt("UTL_POST");
            u.UTL_MAT = res.getInt("UTL_MAT");
            u.UTL_NOM = res.getString("UTL_NOM");
            u.UTL_PRENOM = res.getString("UTL_PRENOM");
            u.UTL_MAIL = res.getString("UTL_MAIL");
            u.UTL_PWD = res.getString("UTL_PWD");
            u.UTL_LOGIN = res.getString("UTL_LOGIN");
            u.UTL_TYPE = res.getInt("UTL_TYPE");
            u.UTL_TEL_B = res.getInt("UTL_TEL_B");
            u.UTL_TEL_MOBILE = res.getInt("UTL_TEL_MOBILE");
            u.STR_CODE = res.getInt("STR_CODE");
            list.add(u);
        }
        }catch(Exception sqle){System.out.println("***********************"+sqle.getMessage());}
        System.out.println("La liste contient :"+ list.size() +" utilisateurs");
        return list;
    }
 
    public static ArrayList<User> SearchByNom(String nom)
    {
        ArrayList<User> list = new ArrayList<User>();
        ConnexionBD db = new ConnexionBD ();
        try{
            ResultSet res = db.executerSelect("select * from "+ User.Table +" where UTL_NOM ='"+ nom +"'");
            while (res.next())
            {
                User u = new User();
                u.UTL_ID = res.getInt("UTL_ID");
                u.UTL_POST = res.getInt("UTL_POST");
                u.UTL_MAT = res.getInt("UTL_MAT");
                u.UTL_NOM = res.getString("UTL_NOM");
                u.UTL_PRENOM = res.getString("UTL_PRENOM");
                u.UTL_MAIL = res.getString("UTL_MAIL");
                u.UTL_PWD = res.getString("UTL_PWD");
                u.UTL_LOGIN = res.getString("UTL_LOGIN");
                u.UTL_TYPE = res.getInt("UTL_TYPE");
                u.UTL_TEL_B = res.getInt("UTL_TEL_B");
                u.UTL_TEL_MOBILE = res.getInt("UTL_TEL_MOBILE");
                u.STR_CODE = res.getInt("STR_CODE");
                list.add(u);
            }
            System.out.println("La liste contient :"+ list.size() +" utilisateurs");
            }catch(Exception sqle){}
        return list;
    }

    public static User SearchByLogin(String login) throws Exception
    {
        User u = null;
        ConnexionBD db = new ConnexionBD ();
        ResultSet res = db.executerSelect("select * from "+ User.Table +" where UTL_LOGIN ='"+ login +"'");
        if (res.next())
        {
        u = new User();
        u.UTL_ID = res.getInt("UTL_ID");
            u.UTL_POST = res.getInt("UTL_POST");
            u.UTL_MAT = res.getInt("UTL_MAT");
            u.UTL_NOM = res.getString("UTL_NOM");
            u.UTL_PRENOM = res.getString("UTL_PRENOM");
            u.UTL_MAIL = res.getString("UTL_MAIL");
            u.UTL_PWD = res.getString("UTL_PWD");
            u.UTL_LOGIN = res.getString("UTL_LOGIN");
            u.UTL_TYPE = res.getInt("UTL_TYPE");
            u.UTL_TEL_B = res.getInt("UTL_TEL_B");
            u.UTL_TEL_MOBILE = res.getInt("UTL_TEL_MOBILE");
            u.STR_CODE = res.getInt("STR_CODE");
        }
        System.out.println("ByLogin : OK");
        return u;
    }
}